<!--
* @author: JT
* @Date: 2024-08-02
* @description: 移动端
* @Last Modified time: 2024-08-02 14:52:31
-->
<template>
  <div class="homePage">
    <SuScrollbar height="100vh" @scroll="scrolling">
      <div class="page-main">
        <header>
          <div class="head-mask" :style="{ opacity: state.opacity }"></div>
          <div class="info-item" title="用户名">
            <i class="fa fa-user-web maR3" />
            <span>{{ state.userName }}</span>
          </div>
          <div class="head-user">
            <div class="info-item blueHover" @click="editPassword">
              <i class="fa fa-password" />
            </div>
            <div class="info-item blueHover" @click="logoutHandle">
              <i class="fa fa-power" />
            </div>
          </div>
        </header>
        <main>
          <section>
            <div class="head-home">
              <div class="home-name">{{ state.appName }}</div>
              <div class="home-remark">{{ state.appRemark }}</div>
            </div>
            <img class="head-img" :src="getImgSrc('home-head-img.png')" />
          </section>
          <div class="app-wrap">
            <div class="menu-app">
              <div
                v-for="item in state.list"
                :key="item.id"
                class="app-item"
                :title="item.remark || ''"
                @click="jumpToApp(item.id)"
              >
                <div class="item-inner">
                  <img
                    class="app-icon"
                    :src="getImgSrc(`app-icon/portal-app${item.portalImg || 1}.png`)"
                    alt=""
                  />
                  <p class="app-name">{{ item.name }}</p>
                </div>
              </div>
            </div>
          </div>
        </main>
      </div>
    </SuScrollbar>
  </div>
  <PasswordModal
    v-model:show="state.showPassword"
    :name="state.userName"
    closable
    @callback="logOut"
  />
</template>

<script setup lang="ts">
import SuScrollbar from 'components/src/su-scrollbar/index.vue';
import PasswordModal from './passwordModal.vue';
import { reactive, onMounted, createVNode } from 'vue';
import { Modal, notification } from 'ant-design-vue';
import { ExclamationCircleOutlined } from '@ant-design/icons-vue';
import { getAssetsFile } from 'commons/src/imgUtil.js';
import loginApi from 'apis/src/portal/login';
import homeApi, { IAppItem } from 'apis/src/portal/home';
import portalCommonApi, { IUserInfo } from 'apis/src/portal/common';
import { getImgSrc } from '../../imgSrc';

const state = reactive({
  appLogo: '', // 项目logo
  appName: '', // 项目名称
  appRemark: '', // 备注
  version: '', // 项目版本号
  departmentName: '', // 所属科室
  userName: '', // 用户名
  opacity: 0,
  list: [] as IAppItem[],
  roles: [] as IUserInfo["roles"],
  showPassword: false,
});
onMounted(() => {
  getAppInfo();
  getUserInfo();
  getList();
});

// 获取系统信息
const getAppInfo = () => {
  loginApi.getPortalInfo().then((res) => {
    state.appLogo = res.data.logUrl || getAssetsFile('app-logo.svg');
    state.appName = res.data.name || '医疗质量监控管理平台';
    state.appRemark =
      res.data.remark ||
      '基于大数据交换处理技术，为医疗质量管理决策提供准确的数据支撑。';
    state.version = res.data.version || 'v1.0.1';
  });
};
// 获取用户信息
const getUserInfo = () => {
  portalCommonApi.getUserInfo().then((res) => {
    const { departmentName, code, roles } = res.data;
    state.departmentName = departmentName;
    state.userName = code;
    state.roles = roles;
  });
};
// 修改密码
const editPassword = () => {
  state.showPassword = true;
};
// 退出系统
const logoutHandle = () => {
  Modal.confirm({
    title: '确定要退出系统吗？',
    icon: createVNode(ExclamationCircleOutlined),
    content: createVNode(
      'div',
      {
        style: 'color:red;',
      },
      '退出后若要操作，需要重新登录'
    ),
    okText: '确定',
    cancelText: '取消',
    onOk() {
      logOut();
    },
  });
};
// ajax退出登录
const logOut = () => {
  loginApi.logOut().then(() => {
    localStorage.removeItem('token');
    localStorage.removeItem('isPwdExpired');
    localStorage.removeItem('encryp');
    location.reload();
  });
};
// 获取列表
const getList = () => {
  homeApi.getAppList().then((res) => {
    const list = res.data || [];
    state.list = list;

    // 如果只有一个项目，则默认直接跳转到项目
    // if (list.length === 1) {
    //   const match = list[0]
    //   const token = localStorage.getItem('token')
    //   const fix = match.homePage.includes('?') ? '&' : '?'
    // 	const url = match.homePage + fix + 'token=' + token
    //   window.location.href = url
    // }
  });
};
// 滚动
const scrolling = ({ y }: {y:number}) => {
  state.opacity = y > 30 ? 1 : 0;
};
// 跳转系统
const jumpToApp = (id: string, _blank?:string) => {
  const match = state.list.find((V) => V.id === id);
  if (match && match.homePage) {
    const token = localStorage.getItem('token');
    const fix = match.homePage.includes('?') ? '&' : '?';
    const url = match.homePage + `${fix}token=${token}`;
    if (_blank) {
      // 新窗口打开链接
      window.open(url, _blank);
    } else {
      window.location.href = url;
    }
  } else {
    notification.error({
      message: '错误警告：',
      description: '无匹配的跳转路径，请联系管理员！',
      duration: 10,
    });
  }
};
</script>
<style lang="scss" scoped>
@import 'commons/src/css/scss/variate.scss';

.homePage {
  width: 100%;
  height: 100%;
  background-color: #f9fafb;
  position: relative;
  overflow-y: hidden;
}

$headHeight: 50px;
.page-main {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  background: url('../../assets/imgs/home-head.png') no-repeat;
  background-size: 100% 50%;

  header {
    width: 100vw;
    height: $headHeight;
    font-size: 15px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    position: fixed;
    z-index: 1000;

    .fa {
      font-size: 16px;
    }

    &:hover {
      .head-mask {
        opacity: 1;
      }
    }

    .head-mask {
      width: 100%;
      height: 100%;
      position: absolute;
      background: #f9fafb;
      box-shadow: 0px 6px 58px 0px rgba(196, 203, 214, 0.2);
      border-bottom: 1px solid #fff;
      transition: all 0.3s;
      opacity: 0;
    }

    .info-item {
      padding-left: 20px;
      position: relative;
      z-index: 11;
    }

    .head-user {
      padding-right: 20px;
      display: inline-flex;
      position: relative;
      z-index: 11;
    }
  }

  section {
    padding-top: $headHeight !important;
    padding: 20px 20px 10px;
    display: flex;
    justify-content: space-between;
    align-items: center;

    .home-name {
      font-size: 20px;
    }

    .home-remark {
      font-size: 13px;
      line-height: 1.3;
    }

    .head-img {
      width: 40%;
    }
  }

  .app-wrap {
    display: flex;
    justify-content: center;
    padding: 20px 0;
  }

  .menu-app {
    width: 360px;
    display: flex;
    flex-wrap: wrap;

    .app-item {
      margin: 10px;
      width: 100px;
      height: 120px;
      background-color: #f4f6f8;
      border-radius: 14px;
      box-shadow: -6px -8px 12px 0 rgba(255, 255, 255, 0.98);
      cursor: pointer;
      transition: all 0.3s;

      &:hover {
        box-shadow: 4px 6px 16px 0 rgb(65 124 233 / 18%) inset;

        .item-inner {
          box-shadow: 5px 6px 16px 0 rgb(30 111 255 / 14%);
          border-color: $blue-color;

          .item-info {
            .app-name,
            .app-tip {
              color: $blue-color;
            }
          }
        }
      }
    }
    .item-inner {
      width: 100%;
      height: 100%;
      padding: 10px;
      border: 1px solid #fff;
      border-radius: 14px;
      box-shadow: 5px 6px 16px 0px rgba(65, 124, 233, 0.14);
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      transition: all 0.3s;
      position: relative;
      overflow: hidden;

      .app-icon {
        display: block;
        width: 44px;
        height: 44px;
        flex-shrink: 0;
      }

      .app-name {
        flex: 1;
        color: #2e4758;
        padding-top: 20px;
        font-size: 12px;
        line-height: 1.3;
        transition: all 0.3s;
      }
    }
  }

  @media screen and (min-width: 380px) and (max-width: 666px) {
    header {
      font-size: 15px;

      .fa {
        font-size: 16px;
      }
    }
    section {
      padding: 20px 26px 10px;

      .home-name {
        font-size: 22px;
      }
      .home-remark {
        font-size: 13px;
      }
    }

    .menu-app {
      width: 360px;
      .app-item {
        margin: 10px;
        width: 100px;
        height: 130px;
        border-radius: 14px;
      }
      .item-inner {
        padding: 10px;
        border-radius: 14px;

        .app-icon {
          width: 46px;
          height: 46px;
        }

        .app-name {
          padding-top: 24px;
          font-size: 13px;
        }
      }
    }
  }
  @media screen and (min-width: 667px) and (max-width: 895px) {
    header {
      font-size: 16px;

      .fa {
        font-size: 17px;
      }
    }
    section {
      padding: 20px 50px 10px;

      .home-name {
        font-size: 23px;
      }
      .home-remark {
        font-size: 14px;
      }
    }

    .menu-app {
      width: 610px;
      .app-item {
        margin: 15px;
        width: 120px;
        height: 140px;
        border-radius: 18px;
      }
      .item-inner {
        padding: 10px;
        border-radius: 18px;

        .app-icon {
          width: 50px;
          height: 50px;
        }

        .app-name {
          padding-top: 25px;
          font-size: 14px;
        }
      }
    }
  }
  @media screen and (min-width: 896px) and (max-width: 1023px) {
    header {
      font-size: 17px;

      .fa {
        font-size: 18px;
      }
    }
    section {
      padding: 20px 60px 10px;

      .home-name {
        font-size: 26px;
      }
      .home-remark {
        font-size: 16px;
      }
    }

    .menu-app {
      width: 810px;
      .app-item {
        margin: 20px;
        width: 160px;
        height: 150px;
        border-radius: 24px;
      }
      .item-inner {
        padding: 15px;
        border-radius: 24px;

        .app-icon {
          width: 56px;
          height: 56px;
        }

        .app-name {
          padding-top: 25px;
          font-size: 15px;
        }
      }
    }
  }
  @media screen and (min-width: 1024px) {
    header {
      font-size: 18px;

      .fa {
        font-size: 19px;
      }
    }
    section {
      padding: 50px 100px 10px;

      .home-name {
        font-size: 38px;
      }
      .home-remark {
        font-size: 20px;
      }
    }

    .menu-app {
      width: 970px;
      .app-item {
        margin: 20px;
        width: 200px;
        height: 160px;
        border-radius: 24px;
      }
      .item-inner {
        padding: 15px;
        border-radius: 24px;

        .app-icon {
          width: 60px;
          height: 60px;
        }

        .app-name {
          padding-top: 30px;
          font-size: 16px;
        }
      }
    }
  }
}
</style>
